/*
 * Copyright 2011 Impetus Infotech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.impetus.teamplan.mb;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.impetus.teamplan.Constants;
import com.impetus.teamplan.dao.TeamPlanDao;
import com.impetus.teamplan.dao.TeamPlanDaoImpl;
import com.impetus.teamplan.entity.Accomplishment;
import com.impetus.teamplan.entity.Member;
import com.impetus.teamplan.web.WebUtils;

/**
 * @author amresh.singh
 *
 */
public class TeamPlanBean {	
	List<Member> members;		

	/**
	 * @param members the members to set
	 */
	public void setMembers(List<Member> members) {
		this.members = members;
	}


	/**
	 * @return the members
	 */
	public List<Member> getMembers() {
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		Member loggedInMember = (Member)session.getAttribute(Constants.LOGGED_IN_MEMBER);
		if(members != null && ! members.isEmpty()) {
			return members;
		} else {
			List<Member> allMembers = new ArrayList<Member>();
			if(Constants.MEMBER_TYPE_ADMIN.equals(loggedInMember.getMemberType())) {
				System.out.println("Admin user logged-in, Retrieving all members");
				TeamPlanDao teamPlanDao = new TeamPlanDaoImpl();		
				allMembers = teamPlanDao.getAllMembers();
				
				for(Member member : allMembers) {
					List<Accomplishment> accomplishments = member.getAccomplishments();
					for(Accomplishment acc : accomplishments) {
						acc.setMonthStr(WebUtils.getMonth(acc.getMonth()) + "' " + acc.getYear());
					}
				}			
								
			} else {
				System.out.println("Team Member logged-in. Details, setting list of members");				
				List<Accomplishment> accomplishments = loggedInMember.getAccomplishments();
				for(Accomplishment acc : accomplishments) {
					acc.setMonthStr(WebUtils.getMonth(acc.getMonth()) + "' " + acc.getYear());
				}
				loggedInMember.setAccomplishments(accomplishments);
				allMembers.add(loggedInMember);		
			}	
			setMembers(allMembers);
			System.out.println("@@@@@@@@@@@@@@@@@@All Members Found@@@@@@@@@@@@@@");
			System.out.println(allMembers);
			return allMembers;
		}
		
		
	}
	
	public String addNextMonthPlan() {
		/*TeamPlanDao dao = new TeamPlanCassandraDao();
		
		String freshMonth = request.getParameter(Constants.FRESH_MONTH);
		String freshYear = request.getParameter(Constants.FRESH_YEAR); 
		
		System.out.println("Submitting new plan for the coming month");
		List<Member> allMembers = TeamPlanUtils.getAllMembers(request);
		for(Member member : allMembers) {
			String memberId = member.getMemberId();
			String nextMonthPlan = request.getParameter(memberId + "_plan");		
			
			List<String> newMonthPlanList = WebUtils.getDataFromTextArea(nextMonthPlan);
			List<Accomplishment> accomplishments = member.getAccomplishments();
			
			
			accomplishments.add(new Accomplishment(WebUtils.getUniqueId(), freshMonth, freshYear, newMonthPlanList));
			member.setAccomplishments(accomplishments);	
			//Update member in database
			
			dao.updateMember(member);
			
		} 
		
		System.out.println("##########After Updating:" + dao.getAllMembers());
		
		
		request.getSession().setAttribute(Constants.ALL_MEMBERS, allMembers);
		
		RequestDispatcher view = request.getRequestDispatcher("jsp/teamPlan.jsp");
		view.forward(request, response);*/
		return null;
	}	
	
	
	
	
	
	
	
}
